package chapter_12_flyweight.sample;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by CharloYao on 2017/3/24.
 */
public class FlyweightFactory {

    private static FlyweightFactory factory = new FlyweightFactory();
    private Map<String,Flyweight> pool = new HashMap<>();

    private FlyweightFactory(){}

    public static FlyweightFactory getInstance(){
        return factory;
    }

    public Flyweight getFlyweight(String key){
        Flyweight flyweight = pool.get(key);
        if(flyweight == null){
            flyweight = new ConcreteFlyweight();
            pool.put(key,flyweight);
        }
        return flyweight;
    }

    public int getPoolLength(){
        return pool.size();
    }
}
